<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>用户管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
</head>

<body>
  <div class="layui-form" lay-filter="layuiadmin-form-privilege" id="layuiadmin-form-privilege"
    style="padding: 20px 30px 0 0;">
    <input type="hidden" name="uid">
    <div class="layui-form-item">
      <label class="layui-form-label">用户名</label>
      <div class="layui-input-block" style="width:251px">
        <input type='text' name='userName' lay-verify="required" placeholder="用户名" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">密码</label>
      <div class="layui-input-block" style="width:251px">
        <input id="password" type='password' name='password' lay-verify="required" placeholder="密码" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
          <label class="layui-form-label">确认密码</label>
          <div class="layui-input-inline" style="width:251px">
            <input id="passwordAgain" type='password' lay-verify="required" placeholder="请再次输入密码" class="layui-input" onchange="checkPass()">
          </div>
          <div class="layui-form-mid" id="hint" style="color: orangered"></div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">站点名</label>
      <div class="layui-input-block" style="width:251px">
        <input type='text' name='stationName' lay-verify="required" placeholder="站点名" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">角色</label>
      <div class="layui-input-block">
        <div id='roleList'></div>
      </div>
    </div>
    <div class="layui-form-item layui-hide">
      <button class="layui-btn" lay-submit lay-filter="user-submit" id="user-submit">提交</button>
    </div>
  </div>

  <script src="../../../layuiadmin/layui/layui.js"></script>
  <script>
    layui.config({
      base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
      index: 'lib/index' //主入口模块
      , my_send: '../myModules/my_send'
    }).use(['index', 'form', 'transfer', 'my_send'], function () {
      var $ = layui.$
        , form = layui.form
        , my_send = layui.my_send
        , transfer = layui.transfer;

      var params = location.search
      , urid = ""
      , uid = ""
      , roleList = []
      , userRoleIdList = [];

      if (params && params !== '') {
         uid = params.substr(params.indexOf('=') + 1);
         $("input[type=password]").removeAttr("lay-verify");

         my_send({
          url: '/user/getUserByUid'
          , type: 'post'
          , data: { 'uid': uid }
          , success: function (obj) {
            if(obj.msg!='error'){
              $('#layuiadmin-form-privilege input').eq(0).val(obj.rows[0].uid);
              $('#layuiadmin-form-privilege input').eq(1).val(obj.rows[0].userName);
              $('#layuiadmin-form-privilege input').eq(4).val(obj.rows[0].stationName);
              var userRoles = obj.rows[0].userRoles;
              if (userRoles.length > 0) {
                  layui.each(userRoles, function (i, n) {
                    userRoleIdList.push(n.urid);
                  })

                  transfer.reload('selectedRoleList', {
                  data: roleList
                  , value: userRoleIdList
              })
              }
            }
          }
        })
      }

      my_send({
          url: "/role/getRoleByParams"
          , type: 'post'
          , data: {
              page: 1
              , limit: 1000
          }
          , success: function (obj) {
              roleList = obj.rows;
              transfer.reload('selectedRoleList', {
                  data: roleList
                  , value: userRoleIdList
              })
          }
      });
      
      transfer.render({
          elem: '#roleList'  //绑定元素
          , id: 'selectedRoleList'
          , title: ['全部角色', '已有角色']
          , showSearch: true
          , parseData: function (res) {
              return {
                  "value": res.urid //数据值
                  , "title": res.roleName //数据标题
              }
          }
      });
    })

    function checkPass() {
      var password = document.getElementById("password").value;
      var passwordAgain = document.getElementById("passwordAgain").value;
      if (password != passwordAgain) {
        document.getElementById('hint').innerHTML = '二次密码不一致，请确认!';
      }else {
        document.getElementById('hint').innerHTML = '';
      }
    }
  </script>
</body>

</html>